<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.deptName" placeholder="部门名称" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.type" placeholder="部门类型" class="filter-item">
        <el-option
          v-for="item in dicts"
          :key="item.itemKey"
          :label="item.itemValue"
          :value="item.itemKey"
        />
      </el-select>
      <div style="float:right;">
        <el-button v-if="checkPermission(['org:dept:add'])" class="filter-item" style="margin-left: 10px;" icon="el-icon-edit" @click="handleCreate('')">新增</el-button>
        <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
      </div>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      row-key="id"
      lazy
      stripe
      :load="load"
      fit
      highlight-current-row
      style="margin-left: 1px;width: 100%;border-top: 1px solid #dfe6ec;border-left: 1px solid #dfe6ec;border-right: 1px solid #dfe6ec;">
      <el-table-column label="部门名称" align="left">
        <template slot-scope="scope">
          <span>{{ scope.row.deptName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="部门类型" align="left">
        <template slot-scope="scope">
          <span v-for="item in dicts">
            <span v-if="item.itemKey == scope.row.type">{{ item.itemValue }}</span>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="left">
        <template slot-scope="scope">
          <span>{{ scope.row.remark }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="left">
        <template slot-scope="scope">
          <el-tag type="success">
            正常
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="left">
        <template slot-scope="scope">
          <span v-if="scope.row.editTime">{{ scope.row.editTime | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="搜索" align="center" width="160" class-name="small-padding fixed-width" v-if="checkPermission(['org:dept:add','org:dept:del','org:dept:edit','org:dept:view'])">
        <template slot-scope="scope">
          <el-tooltip v-if="checkPermission(['org:dept:view'])" class="item action-icon" effect="dark" content="查看详情" placement="top">
            <a><svg-icon icon-class="ljm-view" class="action_icon icon-size" @click="handleView(scope.row)" /></a>
          </el-tooltip>
          <el-tooltip v-if="checkPermission(['org:dept:edit'])" class="item action-icon" effect="dark" content="编辑" placement="top">
            <a><svg-icon icon-class="ljm-edit" class="action_icon icon-size" @click="handleUpdate(scope.row)" /></a>
          </el-tooltip>
          <el-tooltip v-if="checkPermission(['org:dept:del'])" class="item action-icon" effect="dark" content="删除" placement="top">
            <a><svg-icon icon-class="ljm-del" class="action_icon icon-size" @click="handleDelete(scope.row)" /></a>
          </el-tooltip>
          <el-tooltip v-if="checkPermission(['org:dept:add'])" class="item action-icon" effect="dark" content="新增子部门" placement="top">
            <a><svg-icon icon-class="ljm-add" class="action_icon icon-size" @click="handleCreate(scope.row.id)" /></a>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize" @pagination="getList" />
  </div>
</template>

<script>
  import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
  import {delData, getChildDept, getList} from '@/api/org/dept/index'
  import {getItemByDictKey} from '@/api/sys/dict/item/index'

  export default {
  name: 'dept',
  components: { Pagination },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        pageNo: 1,
        pageSize: 10,
        deptName: '',
        type: ''
      },
      dicts: []
    }
  },
  created() {
    this.getDict('dept_type')
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getList(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
      this.listLoading = false
    },
    handleFilter() {
      this.listQuery.pageNo = 1
      this.getList()
    },
    handleCreate(parentId) {
      this.$router.push({ name: 'dept_mgr_add', query: { parentId: parentId }})
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        delData([row.id]).then(() => {
          this.getList()
          this.listLoading = false
        }).catch(
          this.listLoading = false
        )
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleUpdate(row) {
      this.$router.push({ name: 'dept_mgr_edit', query: { id: row.id }})
    },
    handleView(row) {
      this.$router.push({ name: 'dept_mgr_view', query: { id: row.id, view: 'view' }})
    },
    load(tree, treeNode, resolve) {
      getChildDept(tree.id).then(res => {
        if (res.data) {
          resolve(res.data)
        }
      })
    },
    getDict(dictKey) {
      getItemByDictKey(dictKey).then(res => {
        if (res.data) {
          this.dicts = res.data
        }
      })
    }
  }
}
</script>
<style scoped>
  .action-icon{
    padding: 0 5px;
  }
  .icon-size{
    width: 17px;
    height: 17px;
  }
</style>
